package pv1682010.gui;

import java.sql.SQLException;
import java.util.List;
import java.util.ResourceBundle;
import javax.sql.DataSource;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
import pv1682010.Course;
import pv1682010.CourseManager;
import pv1682010.Database;

/**
 *
 * @author Petr
 */
public class CoursesTableModel extends AbstractTableModel {

    private CourseManager cm;
    private ResourceBundle translations;
    private List<Course> courses;

    public CoursesTableModel() {
        translations = Translations.getRb();
        DataSource ds = new Database();
        cm = new CourseManager(ds);
        setCourses();
    }

    @Override
    public int getRowCount() {
        int rowCount = 0;
        rowCount = courses.size();
        return rowCount;
    }

    @Override
    public int getColumnCount() {
        return 4;
    }

    @Override
    public String getColumnName(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return translations.getString("course_id");
            case 1:
                return translations.getString("course_name");
            case 2:
                return translations.getString("course_teachers");
            case 3:
                return translations.getString("course_description");
            default:
                throw new IllegalArgumentException("columnIndex");
        }
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        Course course = courses.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return course.getId();
            case 1:
                return course.getName();
            case 2:
                return course.getTeachers();
            case 3:
                return course.getDescription();
            default:
                throw new IllegalArgumentException("columnIndex");
        }
    }

    @Override
    public void setValueAt(Object value, int row, int col) {

        Course course = courses.get(row);
        if (col == 1) {
            course.setName(value.toString());
        }
        if (col == 3) {
            course.setDescription(value.toString());
        }
        try {
            cm.updateCourse(course);
            setCourses();
            fireTableCellUpdated(row, col);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, translations.getString("err_c_upd"));
        }
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        if (columnIndex % 2 > 0) {
            return true;
        } else {
            return false;
        }
    }

    public void addCourse(Course course) {
        try {
            cm.addCourse(course);
            int lastRow = cm.getAllCourses().size() - 1;
            setCourses();
            fireTableRowsInserted(lastRow, lastRow);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, translations.getString("err_c_ins"));
        }
    }

    public void deleteCourse(Course course) {
        try {
            cm.removeCourse(course);
            int lastRow = cm.getAllCourses().size() - 1;
            setCourses();
            fireTableRowsDeleted(lastRow, lastRow);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, translations.getString("err_c_del"));
        }
    }

    private void setCourses() {
        try {
            courses = cm.getAllCourses();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, translations.getString("err_c_list"));
        }
    }
}
